var path = require('path');
var express = require('express');
var router = require('./router/index')
var app = express();
var ejs = require('ejs')
var veryToken = require('./public/util/veryToken')



//静态文件地址设定
app.use(express.static(path.join(__dirname, 'public')));
// view engine setup
app.set('views', path.join(__dirname, 'view'));
app.engine('html', require('ejs').__express); 
app.set('view engine', 'html');


//跨越问题，可解决拦截
app.all('*', function(req, res, next) {
  //设为指定的域
  res.header('Access-Control-Allow-Origin', "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header('Access-Control-Allow-Credentials', true);
  res.header("X-Powered-By", ' 3.2.1');

  next();
});


// token验证
veryToken(app)

// 应用router路由可做请求和渲染指向路由
router(app)

// 当token 失效返回提示信息
app.use((err, req, res, next) => {
  if(err.status === 401) {
    return res.status(401).redirect('/login')
  }
})

 
app.listen(3000,() => {
    console.log('服务启动成功:'+`http://localhost:3000`)
})
console.log('listening to port 3000');


